package com.easyardemo.tools.manager;

import android.app.Activity;
import android.content.Context;

import java.util.ArrayList;


/***
 * 应用程序Activity管理类：用于Activity管理和应用程序退出
 */
public class AppManager {

    public ArrayList<Activity> activityList = null;
    private static AppManager instance = null;

    /***
     * 单一实例
     */
    public static AppManager getAppManager() {
        if (instance == null) {
            synchronized (AppManager.class) {
                if (instance == null) {
                    instance = new AppManager();
                }
            }
        }

        return instance;
    }

    public int getActivitySize() {
        return activityList.size();
    }

    /***
     * 添加Activity到堆栈
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (activityList == null) {
            activityList = new ArrayList<Activity>();
        }
        activityList.add(activity);
//		System.out.println("activityList---------->" + activityList.toString());
    }

    /***
     * 获取当前Activity
     */
    public Activity currentActivity() {
        if (activityList != null) {
            int size = activityList.size();
            if (size > 0) {
                return activityList.get(size - 1);
            }
        }

        return null;
    }

    /***
     * 结束指定的Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityList.remove(activity);
        }
    }

    /***
     * 结束指定类的Activity
     *
     * @param cls
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : activityList) {
            if (activity != null && activity.getClass().equals(cls)) {
                activityList.remove(activity);
                activity.finish();
                break;
            }
        }
    }

//	/***
//	 * 结束所有Activity
//	 */
//	public void finishAllActivity() {
//		for (Activity activity : activityList) {
//			if (activity != null) {
//				activity.finish();
//			}
//		}
//
//		activityList.clear();
//	}

    /***
     * 结束所有Activity
     */
    public void finishAllActivity() {

        for (int i = activityList.size() - 1; i >= 0; i--) {
            Activity activity = activityList.get(i);
            if (activity != null) {
                activity.finish();
            }
        }

        activityList.clear();
    }

    /***
     * 保留栈顶Activity
     */
    public void saveStackTopActivity() {

        for (int i = activityList.size() - 2; i >= 0; i--) {
            Activity activity = activityList.get(i);
            if (activity != null) {
                activityList.remove(activity);
                activity.finish();
            }
        }

    }

    /***
     * 结束TAB页或它所指定的Activity之上的其他Activity
     */
    public void finishOtherActivity() {
        String name;
        int size = activityList.size();
        for (int i = size - 1; i >= 0; i--) {
            Activity activity = activityList.get(i);
            name = activityList.get(i).getClass().getSimpleName();
            if ("HomeActivity".equals(name)
//					|| name.equals("BargainWebActivity")
//					|| name.equals("OwnerActivity")
//					|| name.equals("BusinessActivity")
                    || "MainActivity".equals(name)
                    || "EAddActivity".equals(name)
                    || "CookActivity".equals(name)) {
                break;
            }

            if (activity != null) {
                activityList.remove(activity);
                activity.finish();
            }
        }
    }

    /***
     * 结束TAB页或它所指定的Activity之上的其他Activity
     */
    public void overdueToLoginActivity() {
        //注销推送
//		BasaeApplication.getmInstance().unRegedistPush();

//        SpManager.clearLoginInfo();

        String name;
        int size = activityList.size();
        for (int i = size - 1; i >= 0; i--) {
            Activity activity = activityList.get(i);
            name = activity.getClass().getSimpleName();
            if ("MainActivity".equals(name) || "HomeActivity".equals(name)) {
                break;
            }

            if (activity != null) {
                activityList.remove(activity);
                activity.finish();
            }
        }

        Context context = currentActivity();
        if (context == null) {
            return;
        }
        //登录（跨模块跳转Activity）
//        RouterCommonUtil.startMainActivity(this,RouteUtils.OWN_ACTIVITY_LOGIN);
    }

    /***
     * 退出应用程序
     */
    public void exitApp() {
        try {
            finishAllActivity();
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
            System.gc();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void destryManager() {
        instance = null;
    }
}
